/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Datos;

import Entidades.Mecanico;
import Interfaces.IMecanico;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author HOUSE
 */
public class DBMecanico implements IMecanico{
private Connection conexion = null;
    public Connection getConection() {
       if (conexion == null) {
           try {
               Driver driver = new com.mysql.jdbc.Driver();
               DriverManager.registerDriver(driver);
               conexion = DriverManager.getConnection("jdbc:mysql://localhost:3306/automotora", "root", "pass1234");
              
           } catch (SQLException ex) {
               ex.printStackTrace();
               Logger.getLogger(DBAutos.class.getName()).log(Level.SEVERE, null, ex);

           }
       }
       return conexion;
   }
    @Override
    public boolean IngresaMecanico(Mecanico nodo) {
       try {
           CallableStatement statement;
           statement = getConection().prepareCall("{call IngresaMecanico (?,?,?,?,?,?,?)}");         
           statement.setInt(1, nodo.getIDMecanico());
           statement.setString(2, nodo.getNombre());
           statement.setInt(3, nodo.getCI());
           statement.setInt(4,nodo.getTelefono());
           statement.setString(5,nodo.getEmail());
           statement.setString(6,nodo.getDireccion());
           statement.setString(7, nodo.getApellido());
           
           
         
           statement.executeUpdate();
        }catch (Exception ex){
            System.out.println("error al ingresar mecanicos");
            ex.printStackTrace();
            return false;
        }
        return true;
    }

    @Override
    public boolean ModificaMecanico(Mecanico nodo) {
         try {
           CallableStatement statement;
           statement = getConection().prepareCall("{call ModificaMecanico (?,?,?,?,?,?,?)}");         
           statement.setInt(1, nodo.getIDMecanico());
           statement.setString(2, nodo.getNombre());
           statement.setInt(3, nodo.getCI());
           statement.setInt(4,nodo.getTelefono());
           statement.setString(5,nodo.getEmail());
           statement.setString(6,nodo.getDireccion());
           statement.setString(7, nodo.getApellido());
           
           
         
           statement.executeUpdate();
        }catch (Exception ex){
            System.out.println("error al ingresar encargados");
            ex.printStackTrace();
            return false;
        }
        return true;
    }

    @Override
    public boolean EliminaMecanico(int IDMecanico) {
        try{
            CallableStatement statement;
            statement=getConection().prepareCall("{call EliminaMecanico(?)}");
            statement.setInt(1,IDMecanico);
            statement.executeUpdate();
            
        }catch(Exception ex){
           System.out.println("error al eliminar mecanico");
            ex.printStackTrace();
            return false;  
        }
        return true;
    }

    @Override
    public ArrayList<Mecanico> ListaMecanico() {
        ArrayList<Mecanico> per = new ArrayList<Mecanico>();
       try {
           CallableStatement statement = getConection().prepareCall("{call ListaMecanico}");
           ResultSet resultSet = statement.executeQuery();
           while (resultSet.next()) {
               Mecanico reglam= new Mecanico();
              
               reglam.setIDMecanico(resultSet.getInt(1));
               reglam.setNombre(resultSet.getString(2));
               reglam.setCI(resultSet.getInt(3));
               reglam.setTelefono(resultSet.getInt(4));
               reglam.setEmail(resultSet.getString(5));
               reglam.setDireccion(resultSet.getString(6));
               reglam.setApellido(resultSet.getString(7));
               
               per.add(reglam);
              
           }
           return per;
       } catch (SQLException ex) {
           ex.printStackTrace();
           return null;
       }
    }

    @Override
    public DefaultTableModel ModeloMecanico() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
    
}
